[bits 16]
[org 0x7c00]
 
xor ax, ax 
mov ax, ds	
jmp enableA20


A20success		db	"A20 enabled!\13\10\0"
failure			db	"FAILURE!\13\10\0"
hello			db	"loading...\13\10\0"
	
gdtr:
		dw		0	
		dd		0	


gdt:		dd		0		
		dd		0 
code_seg:	dw		0xffff
		dw		0x0000
		db		0x10
		db		0x9a
		db 		0xcf
		db		0x00
data_seg:	dw		0xffff
		dw		0x0000
		db		0x10
		db		0x92
		db		0xcf
		db		0x00
video_seg:	dw		0xffff
		dw		0x0000
		db		0x00
		db		0x92
		db		0xcf
		db		0x00
gdt_end:

	

print:	
	;; i took this part from http://www.osdever.net/tutorials/hello_btldr.php
	mov ah, 0xE0	; The function to display a chacter (teletype)
	mov bh, 0x00	; Page number
	mov bl, 0x07	; Normal text attribute

	nextchar:
		lodsb		; this incremeents si
		or al, al	; check if were done
		jz done	
		int 0x10	; Run the BIOS video interrupt 
 		jmp nextchar	; Loop back round tothe top
	done:			; Label at the end to jump to when complete
		 ret		; Return to main program

enableA20:
	cli 
	call a20wait 
	mov al,0xAD 
	out 0x64,al 
	call a20wait 

	mov al,0xD0 
	out 0x64,al 
	call a20wait2 

	in al,0x60 
	push eax 
	call a20wait 

	mov al,0xD1 
	out 0x64,al 
	call a20wait 

	pop eax 
	or al,2 
	out 0x60,al 
	call a20wait 

	mov al,0xAE 
	out 0x64,al 
	call a20wait 
	
	sti 
	jmp A20Enabled

	a20wait: 
		in al,0x64 
		test al,2 
		jnz a20wait 
		ret 

	a20wait2: 
		in al,0x64 
		test al,1 
		jz a20wait2 
		ret

A20Enabled:
	mov si, A20success
	call print

	xor eax, eax
	add eax, gdt
	mov [gdtr + 2], eax
	mov eax, gdt_end
	sub eax, gdt
	mov [gdtr], ax
	lgdt [gdtr]
	

	mov dl, 0		; drive (0 means floppy)
	mov ah, 0x02	; read function
	mov al, 25		; sectors to read
	mov dh, 0		; head
	mov cx, 0x02	; sector
	mov bx, 0xffff
	mov es, bx
	mov bx, 0x0010
	int 0x13

	
	cli

	mov eax, cr0
	or eax, 0x01
	mov cr0, eax

	;; jump to loaded code 
	jmp 0x08:0x00000000
	


; end of bootsector
times 510-($-$$) db 0	; Fill the rest with zeros
dw 0xAA55			; Boot loader signature






